package com.cg.service.impl;

import com.cg.common.PageResult;
import com.cg.mapper.JobMapper;
import com.cg.mapper.JobLikeMapper;
import com.cg.service.JobService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class JobServiceImpl implements JobService {

    @Autowired
    private JobMapper jobMapper;

    @Autowired
    private JobLikeMapper jobLikeMapper;

    @Override
    public List<Map<String, Object>> getKeywords() {
        return jobMapper.getKeywords();
    }

    @Override
    public PageResult<Map<String, Object>> listJobs(String city, Integer expectMinSalary, Integer pageNum, Integer pageSize) {
        Page<Object> page = PageHelper.startPage(Math.max(pageNum,1), Math.max(pageSize,1));
        List<Map<String,Object>> records = jobMapper.pageQuery(city, expectMinSalary);
        return new PageResult<>(page.getTotal(), records, pageNum, pageSize);
    }

    @Override
    public List<Map<String, Object>> getCitySalaryDistribution(String city) {
        return jobMapper.getCitySalaryDistribution(city);
    }

    @Override
    public List<Map<String, Object>> getProductManagerTopSkills() {
        return jobMapper.getProductManagerTopSkills();
    }

    @Override
    public PageResult<Map<String, Object>> listJobsByType(String type, String city, Integer expectMinSalary, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(Math.max(pageNum,1), Math.max(pageSize,1));
        List<Map<String,Object>> records;
        switch (type == null ? "" : type.toLowerCase()){
            case "back":
                records = jobMapper.pageQueryBack(city, expectMinSalary); break;
            case "c":
                records = jobMapper.pageQueryC(city, expectMinSalary); break;
            case "front":
                records = jobMapper.pageQueryFront(city, expectMinSalary); break;
            case "go":
                records = jobMapper.pageQueryGo(city, expectMinSalary); break;
            case "javascript":
                records = jobMapper.pageQueryJavascript(city, expectMinSalary); break;
            case "nodejs":
                records = jobMapper.pageQueryNodejs(city, expectMinSalary); break;
            case "operations":
                records = jobMapper.pageQueryOperations(city, expectMinSalary); break;
            case "python":
                records = jobMapper.pageQueryPython(city, expectMinSalary); break;
            case "test":
                records = jobMapper.pageQueryTest(city, expectMinSalary); break;
            case "web":
                records = jobMapper.pageQueryWeb(city, expectMinSalary); break;
            case "all":
                records = jobMapper.pageQueryAll(city, expectMinSalary); break;
            case "java":
            default:
                records = jobMapper.pageQuery(city, expectMinSalary); break;
        }
        // PageHelper 会自动统计 total
        return new PageResult<>(com.github.pagehelper.PageInfo.of(records).getTotal(), records, pageNum, pageSize);
    }
}


